﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Speedy
{
    public enum CParams : byte
    {
        Server = 0,
        Db = 1,
        Auth = 2,
        user = 3,
        pwd = 4,
        ConStr = 5,
        SenkTime = 6,
        FirmsNo = 7,
        StoreNo = 8,
        Responsibility = 9,
        ProgramType = 10,
        newCustomerRoute = 11,
        newCustomerRouteWeek = 12,
        TemsilciBazindaRoute = 13,
        FaturayiIrsaliyeKaydet = 14
    }

    public class Config
    {
        public string Path { get; set; }
        static Config instance = null;
        Dictionary<CParams, string> parameters;
        private Config()
        {
            parameters = new Dictionary<CParams, string>();
        }
        public static Config GetConfig
        {
            get
            {
                if (instance == null)
                    instance = new Config();
                return instance;
            }
        }

        public void Add(CParams key, string value)
        {
            if (!instance.parameters.ContainsKey(key))
                instance.parameters.Add(key, value);
            else
                instance.parameters[key] = value;
        }
        public void Remove(CParams key)
        {
            if (instance.parameters.ContainsKey(key))
                instance.parameters.Remove(key);
        }
        public void Load()
        {
            if (File.Exists(Path + "\\Ayarlar.ini"))
            {
                StreamReader sr = new StreamReader(Path + "\\Ayarlar.ini");
                while (sr.Peek() > -1)
                {
                    string[] line = sr.ReadLine().Split(';');
                    instance.Add((CParams)Enum.Parse(typeof(CParams), line[0]), line[1]);
                }
                sr.Close();
            }
        }
        public void Save()
        {
            if (Directory.Exists(Path))
            {
                StreamWriter sw = new StreamWriter(Path + "\\Ayarlar.ini");
                foreach (CParams key in instance.parameters.Keys)
                    sw.WriteLine(key.ToString() + ';' + instance.parameters[key].ToString());
                sw.Close();
            }
        }
        public string GetValue(CParams key)
        {
            if (instance.parameters.ContainsKey(key))
            {
                if (key != CParams.ConStr)
                    return instance.parameters[key];
                else
                    return instance.CnnString();
            }
            else
                if (key == CParams.ConStr)
                    return instance.CnnString();
                else
                    return null;
        }

        public string GetValue(CParams key, string defaultValue)
        {
            if (instance.parameters.ContainsKey(key))
            {
                if (key != CParams.ConStr)
                    return instance.parameters[key];
                else
                    return instance.CnnString();
            }
            else
                return defaultValue;
        }

        string CnnString()
        {
            if (instance.GetValue(CParams.Auth) == "Sql")
                return string.Format("Data Source={0};Database={1};user id={2};password={3}", instance.GetValue(CParams.Server), instance.GetValue(CParams.Db), instance.GetValue(CParams.user), instance.GetValue(CParams.pwd));
            else
                return string.Format("Data Source={0};Initial Catalog={1};Integrated Security=SSPI;", instance.GetValue(CParams.Server), instance.GetValue(CParams.Db));
        }

    }

    public class Session
    {
        static Session instance = null;
        Dictionary<string, object> parameters;

        public static Session Get
        {
            get
            {
                if (instance == null)
                    instance = new Session();
                return instance;
            }
        }

        private Session()
        {
            parameters = new Dictionary<string, object>();
        }

        public void Add(string key, object value)
        {
            if (instance.parameters.ContainsKey(key))
                parameters[key] = value;
            else
                instance.parameters.Add(key, value);
        }

        public ICollection<string> Keys
        {
            get { return instance.parameters.Keys; }
        }

        public bool Remove(string key)
        {
            if (instance.parameters.ContainsKey(key))
                return instance.parameters.Remove(key);
            else
                return false;
        }

        public ICollection<object> Values
        {
            get { return instance.parameters.Values; }
        }

        public object this[string key]
        {
            get
            {
                if (instance.parameters.ContainsKey(key))
                    return instance.parameters[key];
                else
                {
                    instance.parameters.Add(key, null);
                    return instance.parameters[key];
                }
            }
            set
            {
                if (instance.parameters.ContainsKey(key))
                    instance.parameters[key] = value;
                else
                    instance.parameters.Add(key, value);
            }
        }

    }
}
